package com.youfang.sixthmedia.ui.searcher;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.gzit.adapter.CustomerListAdapter;
import com.youfang.sixthmedia.R;
import com.youfang.sixthmedia.common.YoufangContext;
import com.youfang.sixthmedia.message.HousefilterResponse;
import com.youfang.sixthmedia.message.HousefilterResponse.HouseType;
import com.youfang.sixthmedia.ui.searcher.SearcherAreaActivity.AreaAdatper;

public class SearcherHTActivity extends BaseSearcherActivity {

	static class HTAdatper extends CustomerListAdapter<HouseType> {

		public HTAdatper(Context context) {
			super(context);
			this.setItemLayout(R.layout.searcher_item);
		}

		@Override
		protected void render(HouseType item, View view) {

			TextView name = (TextView) view.findViewById(R.id.name);
			name.setText(item.housetypename);

			// 标示选中状态
			ImageView sel = (ImageView) view.findViewById(R.id.selectIndicator);

			if (YoufangContext.searcher_curHT == null) {
				if (item.housetypeid == 0) {
					sel.setImageResource(R.drawable.tick);
				} else {
					sel.setImageDrawable(null);
				}
				return;

			}

			if (YoufangContext.searcher_curHT.housetypeid == item.housetypeid) {
				sel.setImageResource(R.drawable.tick);
			} else {
				sel.setImageDrawable(null);
			}

		}

	}

	final HTAdatper adapter = new HTAdatper(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.searcher_ht_activity);

		ListView listview = (ListView) this.findViewById(R.id.listview);
		listview.setAdapter(adapter);

		if (!needReload()) {
			adapter.reload(YoufangContext.getSearcherFilter()
					.getHouseTypeList());
		} else {
			this.remoteLoadHousefilter();
		}

		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Intent intent = new Intent();
				intent.putExtra("data", adapter.getItem(position));
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
	}

	@Override
	protected void onHousefilterLoaded(HousefilterResponse r) {
		adapter.reload(YoufangContext.getSearcherFilter().getHouseTypeList());

	}

}
